//
//  RoundTableViewCell.m
//  LiveEducation
//
//  Created by Liu,Qingcan on 2017/4/20.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import "RoundTableViewCell.h"
//#import "RoundItem.h"
#import "Video+CoreDataClass.h"
#import "UIView+LEBezierPath.h"
#import "Masonry.h"

@interface RoundTableViewCell ()


@property (nonatomic,strong)UILabel *classLabel;
@property (nonatomic,strong)UILabel *timeLabel;
@property (nonatomic,strong)UILabel *nameLabel;
@property (nonatomic,strong)UIButton *watchButton;
@property (nonatomic,strong)NSString *url;


@end

@implementation RoundTableViewCell

+ (instancetype)cellWithRoundItem:(Video *)item
                        tableView:(UITableView *)tableview
                         delegate:(id<RoundCellDelegate>) delegate{
    static NSString *identifier = @"roundCell";
    RoundTableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        
        cell = [[RoundTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.roundItem = item;
    cell.mDelegate = delegate;
    
    
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupViews];
    }
    return self;
}



- (void)setupViews{
    
    [self.contentView addSubview:self.classLabel];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.timeLabel];
    [self.contentView addSubview:self.watchButton];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.classLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(3);
        make.centerY.equalTo(self.contentView);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(50);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_classLabel.mas_right).offset(3);
        make.centerY.equalTo(self.contentView);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(120);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(3);
        make.centerY.equalTo(self.contentView);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(80);
    }];
    
    [self.watchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeLabel.mas_right).offset(3);
        make.centerY.equalTo(self.contentView);
        make.height.mas_equalTo(30);
        make.right.equalTo(self.contentView.mas_right).offset(-8);
    }];
}



- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma event response
- (void)watchButtonDidClicked:(UIButton *)sender{
    if ([self.mDelegate respondsToSelector:@selector(watchButtonSelected:)]) {
        [self.mDelegate watchButtonSelected:self.url];
    }
}

#pragma mark setter

- (void)setRoundItem:(Video *)roundItem{
    if (!roundItem) {
        return;
    }
    self.timeLabel.text = roundItem.round_start_time;
    self.classLabel.text = roundItem.round_name;
    self.nameLabel.text = roundItem.title;
    self.url = roundItem.round_url;
    
//    [self.watchButton setTitle:roundItem.roundUrl forState:UIControlStateHighlighted];
    if ([roundItem.round_url hasPrefix:@"http"]) {
        if ([roundItem.round_url isEqualToString:@"http://19.offcn.comjavascript:;"]) {
             [self.watchButton setTitle:@"暂无回放" forState:UIControlStateNormal];
             self.watchButton.userInteractionEnabled = NO;
        }else{
            self.watchButton.userInteractionEnabled = YES;
        }
    }else{
        self.watchButton.userInteractionEnabled = NO;
        self.watchButton.titleLabel.font = [UIFont systemFontOfSize:11.f];
        [self.watchButton setTitle:@"直播结束" forState:UIControlStateNormal];
        self.watchButton.backgroundColor = [UIColor lightGrayColor];
    }
    
}



#pragma mark getter 

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.numberOfLines = 0;
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        _nameLabel.font = [UIFont boldSystemFontOfSize:11.8f];
        _nameLabel.textColor = [UIColor colorWithRed:42/255.f green:45/255.f blue:47/255.f alpha:0.8f];//黑色
    }
    return _nameLabel;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.numberOfLines = 0;
        _timeLabel.textAlignment = NSTextAlignmentLeft;
        _timeLabel.font = [UIFont boldSystemFontOfSize:10.5f];
        _timeLabel.textColor = [UIColor colorWithRed:42/255.f green:45/255.f blue:47/255.f alpha:0.8f];//黑色
    }
    return _timeLabel;
}


- (UILabel *)classLabel{
    if (!_classLabel) {
        _classLabel = [[UILabel alloc]init];
        _classLabel.numberOfLines = 0;
        _classLabel.textAlignment = NSTextAlignmentCenter;
        _classLabel.font = [UIFont italicSystemFontOfSize:12.f];
        _classLabel.textColor = [UIColor colorWithRed:218/255.f green:112/255.f blue:102/255.f alpha:0.8f];//黑色
    }
    return _classLabel;
}


- (UIButton *)watchButton{
    if (!_watchButton) {
        _watchButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_watchButton setTitle:@"观看" forState:UIControlStateNormal];
//        _watchButton.titleLabel.textColor = [UIColor colorWithRed:34/255.f green:150/255.f blue:63/255.f alpha:1.f];
//        _watchButton.titleLabel.text = @"cccaa";
        
//        [_watchButton setImage:[UIImage imageNamed:@"watchBtnBack"] forState:UIControlStateNormal];
        
        _watchButton.titleLabel.font = [UIFont systemFontOfSize:11.5f];
        
        _watchButton.backgroundColor = [UIColor colorWithRed:34/255.f green:150/255.f blue:63/255.f alpha:0.8f];//绿色
        [_watchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
//        [_watchButton setTintColor:[UIColor colorWithRed:34/255.f green:150/255.f blue:63/255.f alpha:1.f]];
        
//        [_watchButton makeViewLittleCircle:2.f];
        _watchButton.layer.masksToBounds = YES;
        _watchButton.layer.cornerRadius = 2;
        
        [_watchButton addTarget:self action:@selector(watchButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _watchButton;
}


@end
